package com.search.analysis.impl;

import com.search.FxApplication;
import com.search.analysis.Analysis;
import com.search.entity.SearchContentEntity;
import com.search.utils.ChromeDriverUtil;
import com.search.utils.MapAndBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;


@Component
public class JbzjAnalysis implements Analysis {

    @Override
    public String getData(String keyWord) {
        String url = "http://zhannei.baidu.com/cse/site?q=" + keyWord + "&cc=jb51.net&ie=utf";
        ChromeDriverUtil build = ChromeDriverUtil.buildHide(FxApplication.chromedriver);
        ChromeDriver driver = build.getDriver();
        driver.navigate().to(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        Boolean until = wait.until(webDriver -> {
            WebElement element = webDriver.findElement(By.cssSelector("#results .result"));
            return element.isDisplayed();
        });
        if (!until) {
            System.out.println("没有找到元素");
            build.stop();
            return null;
        }
        WebElement element4 = driver.findElement(By.xpath("//*"));
        String source_code = element4.getAttribute("outerHTML");
        build.stop();
        return source_code;
    }

    @Override
    public List<Map<String, Object>> extract(String data) {
        List<Map<String, Object>> list = new CopyOnWriteArrayList<>();
        Document parse = Jsoup.parse(data);
        Elements select = parse.select("#results .result");
        select.stream().parallel().forEach(element -> {
            try {
                Map<String, Object> map = new HashMap<>();
                Element title = element.selectFirst(".c-title a");
                if (title != null) {
                    map.put("title", title.text());
                    map.put("url", title.attr("href"));
                    Element content = element.selectFirst(".c-content .c-abstract");
                    if (content != null) {
                        map.put("content", content.text());
                        map.put("author", "脚本之家");
                        Element time = element.selectFirst(".c-showurl");
                        if(time!=null){
                            String[] str = time.text().split(" ");
                            map.put("time", str.length>1?str[1]:"");
                        }
                        map.put("source", "脚本之家");
                        list.add(map);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("脚本之家查询到的数量:" + list.size());
        return list;
    }

    @Override
    public List<SearchContentEntity> rinse(List<Map<String, Object>> list) {
        List<SearchContentEntity> collect = list.stream().map(map -> {
            if (map.get("url") == null || map.get("title") == null) {
                return null;
            }
            SearchContentEntity searchContentEntity = MapAndBean.mapToDomain(map, SearchContentEntity.class);
            return searchContentEntity;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SearchContentEntity> process(List<SearchContentEntity> list) {

        return list;
    }


}
